package ace.oauth2.provider.base.support;

import ace.oauth2.provider.common.converter.ClientDetailsConverter;
import ace.oauth2.provider.common.properties.OAuth2Property;
import ace.oauth2.provider.common.support.DefaultClientDetails;
import lombok.Builder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;

import java.util.Map;

/**
 * @author Caspar
 * @contract 279397942@qq.com
 * @create 2020/10/20
 * @description
 */
public class DefaultClientDetailsService implements ClientDetailsService {
    private final OAuth2Property oAuth2Property;

    private ClientDetailsConverter clientDetailsConverter;

    private Map<String, ClientDetails> clientDetails;

    private DefaultClientDetails defaultClientDetails;

    @Builder
    private DefaultClientDetailsService(OAuth2Property oAuth2Property,
                                        ClientDetailsConverter clientDetailsConverter,
                                        DefaultClientDetails defaultClientDetails) {
        this.oAuth2Property = oAuth2Property;
        this.clientDetailsConverter = clientDetailsConverter;
        this.defaultClientDetails = defaultClientDetails;
    }

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        DefaultClientDetails clientDetails = this.clientDetailsConverter.toDefaultClientDetailsFrom(oAuth2Property.getDefaultClientProperty());
        clientDetails.setClientId(clientId);
        return clientDetails;
    }
}
